package cn.lysj.framework.foodv2.repository;

import cn.lysj.framework.base.BaseRepository;
import cn.lysj.framework.foodv2.domain.FoodTypeAndFoodContact;
import cn.lysj.framework.foodv2.vo.FoodListVo;
import cn.lysj.framework.foodv2.vo.FoodVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @author drj
 * @description 食物与品类联系数据处理层
 * @create 2018-11-12 11:21:56
 **/
public interface FoodTypeAndFoodContactRepository extends BaseRepository<FoodTypeAndFoodContact> {

    Integer deleteByFoodId(String foodId);

    @Query(value = "select new cn.lysj.framework.foodv2.vo.FoodListVo(c.id,c.name,c.money,c.logo,c.virtualSellNum,c.actSellNum,a.rank,b.id,b.name,c.foodMaterial,c.remark,c.onSale,c.isUse) " +
            "from FoodTypeAndFoodContact a ,FoodType b , Food c where b.storeId LIKE CONCAT('',:storeId,'') and a.foodTypeId LIKE CONCAT('%',:foodTypeId,'%') and c.name LIKE CONCAT('%',:foodName,'%') and c.isUse LIKE concat('',:isUse,'') and a.foodId = c.id and a.foodTypeId = b.id order by b.rank desc,a.rank desc")
    Page<FoodListVo> findFoodListOneFoodTypeId(Pageable pageable,@Param("storeId")String storeId, @Param("foodTypeId")String foodTypeId,@Param("foodName")String foodName,@Param("isUse")String isUse);

    @Query(value = "select new cn.lysj.framework.foodv2.vo.FoodListVo(c.id,c.name,c.money,c.logo,c.virtualSellNum,c.actSellNum,a.rank,b.id,b.name,c.foodMaterial,c.remark,c.onSale,c.isUse) " +
            "from FoodTypeAndFoodContact a ,FoodType b , Food c where b.storeId = ?1 and a.foodId = c.id and a.foodTypeId = b.id and c.isUse = 1 order by b.rank desc,a.rank desc ")
    Page<FoodListVo> findFoodListAllFoodTypeId(Pageable pageable,String storeId);

    @Query(value = "select new cn.lysj.framework.foodv2.vo.FoodVo(b.id,b.name,b.money,b.logo,b.virtualSellNum,b.actSellNum,b.foodMaterial,b.remark,b.onSale) from FoodTypeAndFoodContact a,Food b  where a.foodTypeId = ?1 and a.foodId = b.id and b.isUse = 1 order by a.rank desc ")
    List<FoodVo> obtainFoodInFoodType(String foodTypeId);

    @Query(value = "select new cn.lysj.framework.foodv2.vo.FoodListVo(c.id,c.name,c.money,c.logo,c.virtualSellNum,c.actSellNum,a.rank,b.id,b.name,c.foodMaterial,c.remark,c.onSale,c.isUse) from FoodTypeAndFoodContact a ,FoodType b , Food c where c.name LIKE CONCAT('%',:foodName,'%') and a.foodId = c.id and a.foodTypeId = b.id and c.isUse = 1 and b.storeId = CONCAT('',:storeId,'') order by b.rank desc,a.rank desc")
    Page<FoodListVo> findFoodListAndFoodName(Pageable pageable, @Param("foodName")String foodName,@Param("storeId")String storeId);



}
